package together.news.common.db;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import together.news.common.db.file.FileDBHandler;

public class NewsDataBase {
	public static enum DBType{FILE,GOOGLE,MYSQL}
	public static DBType currentDBType=DBType.FILE;
	private static Map<DBType,DBHandler<? extends CommonData>> handlers=new HashMap<DBType,DBHandler<? extends CommonData>>();
	private DBHandler<? extends CommonData> currentHandler=null;
	static{
		handlers.put(DBType.FILE, new FileDBHandler());
	}
	
	private NewsDataBase(){
		currentHandler = handlers.get(currentDBType);
	}
	private static NewsDataBase instance=new NewsDataBase();
	public static NewsDataBase getInstance(){
		return instance;
	}
	public  void saveDownLoadText(String group,String sourceType, String sourceURL,String content) throws IOException {
		NewsData newsData=new NewsData();
		newsData.setContent(content);
		newsData.setNewsGroup(group);
		newsData.setSourceType(sourceType);
		newsData.setSourceURL(sourceURL);
		currentHandler.insert(newsData);
	}
	public String readTemplateFile(String pageName,String encoding) throws IOException{
		return currentHandler.readTemplate(pageName,encoding);
	}
	public  String readPageDataFile(String pageName,String encoding) throws IOException{
		return currentHandler.readPageDataFile(pageName,encoding);
	}

	public  void writeHtmlGenerated(String pageName,String text,String encoding) throws IOException {
		 currentHandler.writerHtmlGenerated(pageName,text,encoding);
	}
	
	public void copyTemp2Gen(String subFolder) throws IOException{
		 currentHandler.copyTemp2Gen(subFolder);
	}
	
}
